package io.renren.controller;

import io.renren.annotation.Login;
import io.renren.common.utils.RedisUtils;
import io.renren.constant.AppConstant;
import io.renren.entity.AppGexinEntity;
import io.renren.util.RedisKeys;
import io.renren.vo.RespModel;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * @author liuxubo
 * @filename JPushController.java
 * @package com.ise.controller.app
 * @email lxb@isir.net.cn
 * @date 2019/03/05
 * @comment
 * @since 1.8
 */
@RestController("appSimplePushController")
@RequestMapping("/app/gexin")
public class AppPushController {

    @Autowired
    private RedisUtils redisUtils;

    /**
     * 绑定推送
     *
     * @param entity 参数
     * @return 结果
     */
    @Login
    @PostMapping(value = "/login/{applicationId}", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "绑定推送", produces = MediaType.APPLICATION_JSON_VALUE)
    @com.cxytiandi.encrypt.springboot.annotation.Decrypt
    @com.cxytiandi.encrypt.springboot.annotation.Encrypt
    public RespModel login(@RequestBody @Valid AppGexinEntity entity) {
        if (entity.getUserid() == null || StringUtils.isBlank(entity.getUserid())) {
            return new RespModel().getRespModelInstance(AppConstant.PARAM_IS_MISS, "userid", null);
        }

        if (entity.getCid() == null || StringUtils.isBlank(entity.getCid())) {
            return new RespModel().getRespModelInstance(AppConstant.PARAM_IS_MISS, "cid", null);
        }
        String userid = String.valueOf(entity.getUserid());
        String cid = String.valueOf(entity.getCid());
        redisUtils.set(RedisKeys.getUserGexinKey(userid), cid);
        return new RespModel().getRespModelInstance(AppConstant.STATU_SUCCESS, "", null);
    }

    /**
     * 取消绑定推送
     *
     * @param entity 参数
     * @return 结果
     */
    @Login
    @PostMapping(value = "/logout/{applicationId}", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "取消绑定推送", produces = MediaType.APPLICATION_JSON_VALUE)
    @com.cxytiandi.encrypt.springboot.annotation.Decrypt
    @com.cxytiandi.encrypt.springboot.annotation.Encrypt
    public RespModel logout(@Valid AppGexinEntity entity) {
        if (entity.getUserid() == null || StringUtils.isBlank(entity.getUserid())) {
            return new RespModel().getRespModelInstance(AppConstant.PARAM_IS_MISS, "userid", null);
        }

        if (entity.getCid() == null || StringUtils.isBlank(entity.getCid())) {
            return new RespModel().getRespModelInstance(AppConstant.PARAM_IS_MISS, "cid", null);
        }
        String userid = String.valueOf(entity.getUserid());
        String cid = String.valueOf(entity.getCid());
        redisUtils.delete(RedisKeys.getUserGexinKey(userid));
        return new RespModel().getRespModelInstance(AppConstant.STATU_SUCCESS, "", null);
    }
}
